⚡️ Speed up method GeoCoordinate._to_dict by 143%
#92
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 143% (1.43x) speedup for
GeoCoordinate._to_dictinweaviate/collections/classes/types.py⏱️ Runtime :
1.27 milliseconds→524 microseconds(best of166runs)📝 Explanation and details
The optimization replaces Pydantic's
model_dump(exclude_none=True)method with a direct dictionary comprehension that filters outNonevalues fromself.__dict__. This achieves a 142% speedup by eliminating the overhead of Pydantic's serialization machinery.Key changes:
{k: v for k, v in self.__dict__.items() if v is not None}bypasses Pydantic's model dumping processexclude_none=Trueparameter processingWhy this is faster:
model_dump()involves field validation, type conversion, and complex serialization logic that adds ~2.5x overhead__dict__access is a lightweight operation that maps directly to the desired outputGeoCoordinatehas simple float fields (latitudeandlongitude) with Pydantic Field constraints ensuring they're always present and valid, the direct approach is safePerformance characteristics:
The optimization shows consistent 130-360% speedups across all test cases, with particularly strong gains on:
This optimization is ideal for high-frequency coordinate processing where the
_to_dict()method is called repeatedly, as it eliminates unnecessary Pydantic overhead while maintaining the same output format.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-GeoCoordinate._to_dict-mh2ys2xaand push.